const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function get(url, data){
  const app = getApp()
  console.log(app.globalData.defaultUrl+url)
  return new Promise((resolve,reject)=>{
    wx.request({
      url: app.globalData.defaultUrl+url,
      data:data,
      method: 'GET',
      success:function(res){
        resolve(res.data);
      },
      fail:function(err){
        reject(err)
      }
    })
  })
}

function sendRequest(url, method, data) {
  // 获取 token
  let token = wx.getStorageSync('token');
  const app = getApp()
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.globalData.defaultUrl+url,
      data: data,
      method: method, // 使用传入的请求类型参数
      header: {
        'Content-Type': 'application/json;charset=utf-8',
        'token': token // 将 token 添加到请求头中
      },
      success: function (res) {
        resolve(res.data);
      },
      fail: function (error) {
        reject(error);
      }
    });
  });
}




module.exports = {
  formatTime,
  get,
  sendRequest
}
